﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using ExaminationSys.DataAccess.Entity;
using SQLinq;
using Dapper;
using SQLinq.Dapper;

using DapperExtensions;

namespace ExaminationSys.DataAccess.Repository
{
    public class StudentLevelRepository : Repository<StudentLevel>
    {
        public StudentLevelRepository(SqlConnection context)
            : base(context)
        {
            db = context;
        }

        private SqlConnection db;

        public override StudentLevel GetById(dynamic id)
        {
            return base.ObjectSet.Get<StudentLevel>(db, id, null, null);
        }
    }

    public class StudentLevelService
    {
        private StudentLevelService()
        {
        }

        public StudentLevel GetTestLevelByUserId(string userid)
        {
            using (var db = DataContent.DBServer())
            {
                var result = db.Query(from p in new SQLinq<StudentLevel>()
                                      where p.UserID == userid
                                      select p).FirstOrDefault();
                return result;

            }
        }

        public int InsertOrUpdate(StudentLevel entity)
        {
            using (var db = DataContent.DBServer())
            {
                IUnitOfWork work = new UnitOfWork(db);
                var obj = new
                              {
                                  ID = entity.ID,
                                  UserID = entity.UserID,
                                  TestLevel = entity.TestLevel
                              };

                var dynamicParams = new DynamicParameters(obj);
                dynamicParams.Add("retcode", null, null, ParameterDirection.ReturnValue, 32);
                var i=db.Execute("sp1_StudentLevel_i", dynamicParams, null, null, CommandType.StoredProcedure);
                var result = dynamicParams.Get<int>("retcode");
                return result;
            }
        }
    }
}
